import { State } from "framework/FSM/AState";
import { ActionArgHelper } from "../../../ActionManager/ActionArgHelper";
import { EntityHelper } from "utils/entity/EntityHelper";
import { PositionHelper } from "utils/position/PositionHelper";
 

/**
 防守任务
 */ 
export class DefendTask<T extends IStateArg<Creep>>  extends State<T>
{

    //
    public  getActions(arg:T):Action[]
    {
        
        const memory = arg.entity.getNodeMemory();
        if(!memory.taskid)
        {
            return [];
        }
        const task =arg.task as ITask<ITeamTaskData<"defend">>;
        if(!task)
        {
            // 查找不到任务
            return []
        }
        const creep = arg.entity;
       
        const workroom = Game.rooms[task.data.room];
        if(workroom && (task.data.arg.enemy || task.data.arg.structures))
        {   
            let target:Id<AttackObject>|undefined
            if(task.data.arg.enemy.length)
            {
                target =  task.data.arg.enemy[0];
            }
            else if(task.data.arg.structures.length)
            {
                target =  task.data.arg.structures[0];
            }
            if(target)
            {
                const obj =  Game.getObjectById(target) as AttackObject
                if(obj)
                {
                    if(creep.bodyCount(ATTACK))
                    {
                        return [ActionArgHelper.actionAttack(creep,obj,"attackRed")];
                    }
                    else
                    {
                        return [ActionArgHelper.actionAttack(creep,obj,"attackBlue")]
                    }
                   
                }
                
            }
        }
        if(creep.hits<creep.hitsMax)
        {
            creep.heal(creep);
        }
        else
        {
            creep.healAroundCreep();
        }
        return  [ ActionArgHelper.actionMoveto(creep,{pos:PositionHelper.Create({x:25,y:25,roomName:task.data.room}),range:10},{})]
    }
}
